public without sharing class LeadDedupeHelper {
       //static var to prevent recursive trigger updates
       //validating a change in dev salesforce! 
        private static boolean IsRecursive = false;
        public static boolean IsRecursive() {     
           return isRecursive;
        }        
 /*          
         public static void setRecursive(boolean value) {     
           isRecursive=value;
        }
        
        public static ID LocalAdsLeadRecordType;
        public static ID LocalAdsAccountRecordType; 
        public static ID MarketGroupQueueId ;
        
        public static ID GetLocalAdsLeadRecordType() {
            //Initialize all RT if calling for first time           
            if( LocalAdsLeadRecordType == null ) 
                   GetRecordTypes();
                            
          return LocalAdsLeadRecordType;
            
        }
         
        //retrieve the record types for lead and account for customer care
        public static void  GetRecordTypes()
        {     
            if(LocalAdsLeadRecordType == null || LocalAdsAccountRecordType == null || MarketGroupQueueId == null )
            {
                RecordTypeHelper.PrepareToLoad('Lead');                             
                RecordTypeHelper.PrepareToLoad('Account');                              
                LocalAdsLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
                LocalAdsAccountRecordType = RecordTypeHelper.GetRecordTypeID('Account', 'Local OOR Customer');

            QueueSobject MarketGroupQueue= [Select QueueId from QueueSobject q where sobjecttype='Lead'  and queue.name= 'Market Groups Queue'];
            MarketGroupQueueId = MarketGroupQueue.QueueId;
            
            }
        }
        //assign the owner from employee id 
        public static void AssignOwner(List<Lead> leads, Map<ID, Lead> oldLeads, boolean IsInsert)
        {
            
            List<String> ownerAFrontierEmployeeIDs = new List<String>();                
            if(IsInsert)    {
               //collect all AFEmpid's from the current set of leads being imported
               for (Lead lead : leads) {
                     if (lead.RecordTypeid == LocalAdsLeadRecordType && lead.AFrontier_Employee_ID__c != null ){
                        ownerAFrontierEmployeeIDs.add(lead.AFrontier_Employee_ID__c);
                     }                   
               }            
                  
               //owner lookup 
               Map<string,Id> ownerMap = new Map<string,Id>(); 
               for(List<User> ownerList:[Select AFrontier_Employee_ID__c,Id From User u where u.AFrontier_Employee_ID__c in :ownerAFrontierEmployeeIDs ])               {
                    for(User owner:ownerList){
                       ownerMap.put(owner.AFrontier_Employee_ID__c, owner.Id);           
                    }                                
               }
               //loop thru the current set of leads and update the owner 
               for (Lead lead : leads) {                                   
                 
                //local advertiser lead and leads uploaded thru bulk upload! (no leadsource )
                  if(lead.RecordTypeid == LocalAdsLeadRecordType && lead.LeadSource == null ){
                       if(ownerMap.containsKey(lead.AFrontier_Employee_ID__c))                    {                                                        
                           lead.OwnerId = ownerMap.get(lead.AFrontier_Employee_ID__c);                         
                      }
                      else{                           
                            //if owner not found , throw errror 
                            lead.AFrontier_Employee_ID__c.addError('Af Employee Id not found '+ lead.AFrontier_Employee_ID__c);
                      }
                  }
               }
            }
                    
        }
                
        public static void MoveStandardAddress(List<Lead> leads, Map<ID, Lead> oldLeads, boolean IsInsert)
        {       
            if(IsInsert)    {
               for (Lead lead : leads) {
                     if (lead.RecordTypeid == LocalAdsLeadRecordType && lead.LeadSource == 'Salesgenie.com'  ){
                        if(lead.Street !=null ) lead.Business_Address_L1__c = lead.Street;
                        if(lead.City != null) lead.City__c = lead.City;
                        if(lead.State != null) lead.State_Province__c = lead.State;                            
                        if(lead.PostalCode != null) lead.Zip_Postal_Code__c= lead.PostalCode;                                                
                     }//if                   
               }//for
            }//if           
        }
                
        //throw error if an account with same phone is found!       
        public static void DeDupeAccountPhone(List<Lead> leads, Map<ID, Lead> oldLeads, boolean IsInsert)
        {
           Map<String, Lead> leadPhoneDupeMap = new Map<String, Lead>();
          
           for (Lead lead : leads) {
              string CleanLeadPhone =  SilCustomerHelper.CreatePhoneNumberHash(lead.Phone); 
              // 1. Build the map of phone changes
              if (lead.RecordTypeid == LocalAdsLeadRecordType && lead.Phone != null && CleanLeadPhone != null && 
                        (IsInsert ||  CleanLeadPhone != oldLeads.get(lead.Id).CleanPhoneText__c ) && lead.Duplicate_Status__C != 'Triaged Unique') {    
                  leadPhoneDupeMap.put(CleanLeadPhone, lead);               
                 
            }
        }
         if(leadPhoneDupeMap.size() > 0  ){             
               LeadDedupeHelper.FindExistingAccountsPhone(leadPhoneDupeMap);
         }  
        } 
             
        // find all the leads in
        // the database having the same Phone number as any
        // of the leads being inserted or updated.  
        private static void FindExistingAccountsPhone(Map<String, Lead> leadPhoneDupeMap)
        {                               
            for (Account account : [SELECT Phone, CleanPhoneText__c FROM Account WHERE CleanPhoneText__c IN :leadPhoneDupeMap.KeySet() and RecordTypeid = :LocalAdsAccountRecordType ]) {
                 Lead newLead = leadPhoneDupeMap.get(account.CleanPhoneText__c);
                 newLead.Phone.addError('An account with this Phone Number already exists.');
            
            }
        }
        //called after insert ,update of leads
        //the reason why we are doing this in after trigger is that Id of lead object is not available in the before triggger
        public static void DeDupeLeadAccount(Map<ID,Lead> leads, Map<ID,Lead> oldLeads ,boolean IsInsert)
        {               
             Set<ID> bizNameLeads = new Set<ID>();
             Set<ID>  phoneNumLeads = new Set<ID>();
             
             Set<string> bizNames = new Set<string>();      
             Set<string>  phones= new Set<string>(); 
                             
             //get the newly inserted leads..
             //List<Lead> triggerRecords = [select id,DupingLeadID__c,IsDupe__C,Company, Phone,RecordTypeid,Duplicate_Status__c from lead where id IN :leads.keySet() and RecordTypeid = :LocalAdsLeadRecordType ];
                   
             for (Lead lead : leads.values()){              
               //store phone numbers..
               if (lead.Phone!= null && lead.Duplicate_Status__C != 'Triaged Unique' && (IsInsert || (lead.Phone != oldLeads.get(lead.Id).Phone)))   {                                                 
                       phoneNumLeads.add(lead.Id);                       
                       phones.add(lead.Phone);                                     
               }
               
               if (lead.Company != null && lead.Duplicate_Status__C != 'Triaged Unique' && (IsInsert || (lead.Company != oldLeads.get(lead.Id).Company)))   {                                                  
                       bizNameLeads.add(lead.Id);                       
                       bizNames.add(lead.Company.toLowerCase());                             
               }
             }
                
                //check existing leads/account in db if any company matches
                if(bizNameLeads.size() > 0 || phoneNumLeads.size() > 0  )   {
                        UpdateLeads.doUpdate(bizNameLeads, bizNames, phoneNumLeads, phones, LocalAdsLeadRecordType,LocalAdsAccountRecordType);                               
                }
        }
        
     public static void ConvertLead(List<Lead> leads, Map<ID, Lead> oldLeads, boolean IsInsert)
     {
       for (Lead lead : leads) {
          if(lead.IsConverted){                                   
            //check if account exist with the same company name and phone.. 
            //Convert will be always for single leads.. so this proc is not bulkified!
            //this validation is already happening on convert click button!
            
          //  string retVal = AccountService.searchbyPhoneandName(lead.company,lead.CleanPhoneText__c);
          //  if(retVal == 'phone'){
          //        lead.addError('An Account with this phone number already exists.');
          //  }
          //  else if(retVal == 'name' && lead.Duplicate_Status__c != 'Triaged Unique'){
          //         lead.addError('An Account with this name exists.');
          //  }
           
            LeadDedupeHelper.IsRecursive = true;
            //if phone number matches with other leads.. 
            //find all matching leads with the current lead ph number exclding the current lead
            
            String[] toEmailId =  new String[] {};                       
            List<Lead> updateLeads = new List<Lead>();              
                 
                for (Lead dupePhoneLead : [select id, company, CleanPhoneText__c, Email, Status, OwnerId, Owner.Id, Owner.Email from lead where CleanPhoneText__c != null and RecordType.Name = 'Local Advertiser' and RecordType.SobjectType = 'Lead' and CleanPhoneText__c = :lead.CleanPhoneText__c and isconverted=false and id <> :lead.id]) {                    
                    //update status as No-sale-Duplicate for all matching leads                 
                    if(dupePhoneLead.Status != 'No Sale - Duplicate')  {
                          dupePhoneLead.Status = 'No Sale - Duplicate';
                          updateLeads.Add(dupePhoneLead);                                                     
                          String toEmail = dupePhoneLead.Owner.Email;
                          if (toEmail != null) 
                            toEmailId.Add(toEmail);   
                                                                         
                    }
                     
                }
                
                if(updateLeads.size() > 0 ) 
                      Update updateLeads;                 
                               
             
                
                LeadDedupeHelper.IsRecursive= false;
                 //send email to all lead owners...
                 if(toEmailId.size()>0)          {                    
                      String subject='Lead Converted';                    
                      String[] ccAddresses = new String[] {};
                      String bodyText= '"<b>'+ lead.Company +'</b>" with phone number:<b> ' + lead.Phone +' </b> has been converted ' +
                         'to an account by another Account Manager. Your lead now has the status <b>No Sale- Duplicate</b>.<p>' +  
                         'Contact your DSM for any questions.';                                   
                      integer bodyTextType= 2; //html                                                                 
                      EmailHelper.SendMessage(subject, toEmailId, ccAddresses, bodyText, bodyTextType);
                                              
                 }   
          }
                                    
             }
     }*/
     
    }